package com.demo.controller;

import com.demo.entity.User;
import com.demo.service.UserService;
import com.demo.dao.UserDao;  // 假设你有一个 UserDao 用于访问数据库
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
class UserControllerIT {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    @BeforeEach
    void setupTestData() {
        // 清空数据库中的用户数据
        userDao.deleteAll();

        // 插入测试用户数据
        User user = new User();
        user.setUserID("u123");
        user.setPassword("pass");
        user.setIsadmin(0); // 普通用户
        user.setPicture("picture");
        userDao.save(user);

        User admin = new User();
        admin.setUserID("admin");
        admin.setPassword("pass");
        admin.setIsadmin(1); // 管理员用户
        admin.setPicture("./picture");
        userDao.save(admin);
    }

    @Test
    void testLogin_UserSuccess() throws Exception {
        mockMvc.perform(post("/loginCheck.do")
                        .param("userID", "u123")
                        .param("password", "pass"))
                .andExpect(status().isOk())
                .andExpect(content().string("/index"));
    }

    @Test
    void testLogin_AdminSuccess() throws Exception {
        mockMvc.perform(post("/loginCheck.do")
                        .param("userID", "admin")
                        .param("password", "pass"))
                .andExpect(status().isOk())
                .andExpect(content().string("/admin_index"));
    }

    @Test
    void testLogin_Failure() throws Exception {
        mockMvc.perform(post("/loginCheck.do")
                        .param("userID", "bad")
                        .param("password", "user"))
                .andExpect(status().isOk())
                .andExpect(content().string("false"));
    }

    @Test
    void testRegister() throws Exception {
        mockMvc.perform(post("/register.do")
                        .param("userID", "u456")
                        .param("userName", "Test User")
                        .param("password", "123456")
                        .param("email", "test@example.com")
                        .param("phone", "1234567890"))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("login"));
    }

    @Test
    void testCheckPassword_True() throws Exception {
        mockMvc.perform(get("/checkPassword.do")
                        .param("userID", "u123")
                        .param("password", "pass"))
                .andExpect(status().isOk())
                .andExpect(content().string("true"));
    }

    @Test
    void testCheckPassword_False() throws Exception {
        mockMvc.perform(get("/checkPassword.do")
                        .param("userID", "u123")
                        .param("password", "wrongpass"))
                .andExpect(status().isOk())
                .andExpect(content().string("false"));
    }

    @Test
    void testUpdateUser() throws Exception {
        MockMultipartFile mockFile = new MockMultipartFile(
                "picture",
                "test.jpg",
                "image/jpeg",
                "test image content".getBytes()
        );

        mockMvc.perform(multipart("/updateUser.do")
                        .file(mockFile)
                        .param("userID", "u123")
                        .param("userName", "New Name")
                        .param("passwordNew", "newpass")
                        .param("email", "new@example.com")
                        .param("phone", "12345"))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("user_info"));
    }
}
